{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "ICS 104 - Introduction to Programming in Python and C  \n",
    "# Decision Structures\n",
    "## Reading Assignment\n",
    "- Chapter 3 Sections 1, 2, 3, 4, 7, 8 and 9."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Chapter Learning Outcomes\n",
    "### At the end of this chapter, you will be able to"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- To implement decisions using if statements\n",
    "- To compare integers, floating-point numbers, and strings\n",
    "- To write statements using Boolean expressions\n",
    "- To validate user input"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The if Statement\n",
    "- A computer program often needs to make decisions based on input, or circumstances. \n",
    "- The <font color='blue'>**if statement**</font> allows a program to carry out different actions depending on the nature of the data to be processed. "
   ]
  },
  {
   "attachments": {
    "fig1.png": {
     "image/png": "[encoded data, 543664 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig1.png](attachment:fig1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The <font color='blue'>**if statement**</font> is used to implement a decision. \n",
    "- The two keywords of the <font color='blue'>**if statement**</font> are:\n",
    "    - *if*\n",
    "    - *else*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- When a condition is fulfilled, one set of statements is executed.\n",
    "- Otherwise, another set of statements is executed. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The if Statement - Example\n",
    "- In some countries, the number 13 is considered unlucky. \n",
    "    - To avoid offending tenants, buildings owners sometimes skip the 13th floor; floor 12 is immediately followed by floor 14. \n",
    "    - Of course, floor 13 is not left empty, it is simply called floor 14."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The computer that controls the building elevators needs to compensate this foible and adjust all floor numbers above 13.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The if Statement - Example\n",
    "- Let’s simulate this process in Python. \n",
    "    - We will ask the user to type in the desired floor number and then compute the actual floor. \n",
    "    - When the input is above 13, then we need to decrement the input to obtain the actual floor."
   ]
  },
  {
   "attachments": {
    "fig2.png": {
     "image/png": "[encoded data, 339848 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig2.png](attachment:fig2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The if Statement - Example\n",
    "- For example, if the user provides an input of 20, the program determines the actual floor as 19. \n",
    "- Otherwise, we simply use the supplied floor number."
   ]
  },
  {
   "attachments": {
    "fig3.png": {
     "image/png": "[encoded data, 44672 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig3.png](attachment:fig3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The figure is called a **flowchart**. \n",
    "- A **flowchart** shows the structure of decisions and tasks that are required to solve a certain problem (usually a complex one).\n",
    "- Flowcharts help the programmer to visualize the flow of control. "
   ]
  },
  {
   "attachments": {
    "fig16.PNG": {
     "image/png": "[encoded data, 22220 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Elements of a Flowchart\n",
    "![fig16.PNG](attachment:fig16.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The if Statement - Example\n",
    "- Sometimes, it happens that there is nothing to do in the else branch of the statement. \n",
    "    - In that case, you can omit it entirely, such as in this example:"
   ]
  },
  {
   "attachments": {
    "fig4.png": {
     "image/png": "[encoded data, 56192 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig4.png](attachment:fig4.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The Compound if Statement\n",
    "- Some constructs in Python are **compound** statements, which span **multiple lines** and consist of a ***header*** and a ***statement block***. \n",
    "- In **Compound** statements,\n",
    "    - the header requires a **colon** (**:**) at the end of it, and\n",
    "    - the statement block consisting of a group of one or more statements, is such that each statement is indented to the **same indentation level**. \n",
    "- A **statement block** begins on the line following the header and ends at the first statement indented less than the first statement in the block. \n",
    "- Any number of spaces can be used to indent statements within a block, but all statements within the block must have the same indentation level. \n",
    "- Note that **comments** are not statements and thus can be indented to any level."
   ]
  },
  {
   "attachments": {
    "fig5.png": {
     "image/png": "[encoded data, 200052 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# if Statement - Syntax\n",
    "![fig5.png](attachment:fig5.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "##\n",
    "#  This program simulates an elevator panel that skips the 13th floor.\n",
    "#\n",
    "\n",
    "# Obtain the floor number from the user as an integer.\n",
    "floor = int(input(\"Floor: \"))\n",
    "\n",
    "# Adjust floor if necessary.\n",
    "if floor > 13 :\n",
    "   actualFloor = floor - 1\n",
    "else :\n",
    "   actualFloor = floor\n",
    "\n",
    "# Print the results\n",
    "print(\"The elevator will travel to the actual floor\", actualFloor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "# Student Activity\n",
    "- In some Asian countries, the number 14 is considered unlucky. Some building owners play it safe and skip both the thirteenth and the fourteenth floor. How would you modify the sample program to handle such a building?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "## Enter your code here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Relational Operators\n",
    "- Every if statement contains a condition.\n",
    "- In many cases, the condition involves comparing two values. \n",
    "    - For example, in the previous examples we tested floor > 13.\n",
    "- The comparison > is called a <font color='blue'>**relational operator**</font>."
   ]
  },
  {
   "attachments": {
    "fig6-1.PNG": {
     "image/png": "[encoded data, 13572 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig6-1.PNG](attachment:fig6-1.PNG)"
   ]
  },
  {
   "attachments": {
    "fig6.png": {
     "image/png": "[encoded data, 123720 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig6.png](attachment:fig6.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Relational Operators \n",
    "## Assignment vs. Equality Testing\n",
    "- In Python, <font color='blue'>**=**</font> already has a meaning, namely **assignment**.\n",
    "- The <font color='blue'>**==**</font> operator denotes **equality testing**:\n",
    "    - floor = 13  (<font color='blue'>**# Assign 13 to floor**</font>)\n",
    "    - if floor == 13 :  (<font color='blue'>**# Test whether floor equals 13**</font>)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Comparing Strings \n",
    "- Strings can also be compared using Python’s relational operators. \n",
    "    - For example, to test whether two strings are equal, use the <font color='blue'>**==**</font> operator.\n",
    "    - or to test if they are not equal, use the <font color='blue'>**!=**</font> operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name1 = input(\"Enter the first name \")\n",
    "name2 = input(\"Enter the second name \")\n",
    "if name1 == name2:\n",
    "    print(\"The strings '\"+ name1 + \"' and '\" + name2 + \"' are identical.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name1 = input(\"Enter the first name \")\n",
    "name2 = input(\"Enter the second name \")\n",
    "if name1 != name2:\n",
    "    print(\"The strings '\"+ name1 + \"' and '\" + name2 + \"' are not identical.\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## When are two strings equal?\n",
    "- For two strings to be equal, they must be of the same length and contain the same sequence of characters:"
   ]
  },
  {
   "attachments": {
    "fig8.png": {
     "image/png": "[encoded data, 11968 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig8.png](attachment:fig8.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- If even one character is different, the two strings will not be equal:"
   ]
  },
  {
   "attachments": {
    "fig9.png": {
     "image/png": "[encoded data, 60440 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig9.png](attachment:fig9.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Student Activity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(3 <= 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(3 =< 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(3 > 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(4 < 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(4 <= 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(3!=5-3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(3=6/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(1.0 / 3.0 == 0.333333333)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(3<=4<5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"AB\" < \"AC\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print('AB' == chr(65)+chr(66))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"10\" > 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "s1 = \"This is a long string.\"\n",
    "s2 = \"This is a l0ng string.\"\n",
    "if s1==s2:\n",
    "    comparison = \"identical\"\n",
    "else:\n",
    "    comparison = \"not identical\"\n",
    "print (\"The string s1 and s2 are\",comparison)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Nested Branches\n",
    "- It is often necessary to include an <font color='blue'>**if statement**</font> inside another. Such an arrangement is called a <font color='blue'>**nested set of statements**</font>.\n",
    "    - Nested decisions are required for problems that have multiple levels of decision making."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Nested Branches (Example)\n",
    "- In the United States, different tax rates are used depending on the taxpayer’s marital status. There are different tax schedules for single and for married taxpayers. Married taxpayers add their income together and pay taxes on the total. \n"
   ]
  },
  {
   "attachments": {
    "fig10.png": {
     "image/png": "[encoded data, 178732 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig10.png](attachment:fig10.png)"
   ]
  },
  {
   "attachments": {
    "fig11.png": {
     "image/png": "[encoded data, 66948 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Flowchart of the Example\n",
    "![fig11.png](attachment:fig11.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "#  This program computes income taxes, using a simplified tax schedule.\n",
    "# Initialize constant variables for the tax rates and rate limits.\n",
    "RATE1 = 0.10\n",
    "RATE2 = 0.25\n",
    "RATE1_SINGLE_LIMIT = 32000.0\n",
    "RATE1_MARRIED_LIMIT = 64000.0     \n",
    "# Read income and marital status     \n",
    "income = float(input(\"Please enter your income: \"))\n",
    "maritalStatus = input(\"Please enter s for single, m for married: \")\n",
    "# Compute taxes due.\n",
    "tax1 = 0\n",
    "tax2 = 0\n",
    "if maritalStatus == \"s\" :\n",
    "   if income <= RATE1_SINGLE_LIMIT :\n",
    "      tax1 = RATE1 * income\n",
    "   else :\n",
    "      tax1 = RATE1 * RATE1_SINGLE_LIMIT\n",
    "      tax2 = RATE2 * (income - RATE1_SINGLE_LIMIT)\n",
    "else :\n",
    "   if income <= RATE1_MARRIED_LIMIT :\n",
    "      tax1 = RATE1 * income\n",
    "   else : \n",
    "      tax1 = RATE1 * RATE1_MARRIED_LIMIT\n",
    "      tax2 = RATE2 * (income - RATE1_MARRIED_LIMIT)\n",
    "        \n",
    "totalTax = tax1 + tax2\n",
    "# Print the results.\n",
    "print(\"The tax is $%.2f\" % totalTax)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Nested Branches - Student Activity\n",
    "- Write a program that reads an integer and prints whether it is `negative`, `zero`, or `positive`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Multiple Alternatives\n",
    "- Multiple if statements can be combined to evaluate complex decisions. \n",
    "- For example, consider a program that displays the effect of an earthquake, as measured by the Richter scale"
   ]
  },
  {
   "attachments": {
    "fig12.png": {
     "image/png": "[encoded data, 127744 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![fig12.png](attachment:fig12.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Multiple Alternatives\n",
    "- You could use multiple <font color='blue'>**if statements**</font> to implement multiple alternatives, like this: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "richter = 8.0\n",
    "if richter >= 8.0:\n",
    "    print(\"Most structures fall\")\n",
    "else:\n",
    "    if richter >= 7.0:\n",
    "        print(\"Many building destroyed\")\n",
    "    else:\n",
    "        if richter >= 6.0:\n",
    "            print(\"Many buildings considerably damaged, some collapse\")\n",
    "        else:\n",
    "            if richter >= 4.5:\n",
    "                print(\"Damage to poorly constructured buildings\")\n",
    "            else:\n",
    "                print(\"No destruction of buildings\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- but this becomes difficult to read and, as the number of branches increases, the code begins to shift further and further to the right due to the required indentation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Multiple Alternatives\n",
    "- Python provides the special construct <font color='blue'>**elif**</font> for creating <font color='blue'>**if**</font> statements containing multiple branches. \n",
    "- Using the <font color='blue'>**elif**</font> statement, the code segment can be rewritten as:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "richter = 8.0\n",
    "if richter >= 8.0:\n",
    "    print(\"Most strcutures fail\")\n",
    "elif richter >= 7.0:\n",
    "    print(\"Many buildings destroyed\")\n",
    "elif richter >= 6.0:\n",
    "    print(\"Many buildings considerably damaged, some collapse\")\n",
    "elif richter >= 4.5:\n",
    "    print(\"Damage to poorly constructed buildings\")\n",
    "else:\n",
    "    print(\"No destruction of buildings\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- As soon as one of the four tests succeeds, the effect is displayed, and no further tests are attempted. \n",
    "- If none of the four cases applies, the <font color='blue'>**final else clause**</font> applies, and a default message is printed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Multiple Alternatives\n",
    "- Here you must sort the conditions and test against the largest cutoff first.\n",
    "- Suppose we reverse the order of tests:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "richter = 7.1\n",
    "if richter >= 4.5: #Tests in worng order\n",
    "    print(\"Damage to poorly constrcuted buildings\")\n",
    "elif richter >= 6.0:\n",
    "    print(\"Many buildings considerably damaged, some collapse\")\n",
    "elif richter >= 7.0:\n",
    "    print (\"Many buildings destroyed\")\n",
    "elif richter >= 8.0:\n",
    "    print (\"Most structures fail\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The remedy is to test the more specific conditions first. \n",
    "    - Here, the condition <font color='blue'>**richter >= 8.0**</font> is more specific than the condition <font color='blue'>**richter >= 7.0**</font>, \n",
    "    - and the condition <font color='blue'>**richter >= 4.5**</font> is more general (that is, fulfilled by more values) than either of the first two."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Multiple Alternatives\n",
    "- In this example, it is also important that we use an <font color='blue'>**if/elif**</font> sequence, not just multiple independent if statements. \n",
    "- Consider this sequence of independent tests."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "richter = 7.1\n",
    "if richter >= 8.0: #Didn't use else\n",
    "    print(\"Most structures faill\")\n",
    "if richter >= 7.0:\n",
    "    print(\"Many buildings destroyed\")\n",
    "if richter >= 6.0:\n",
    "    print(\"Many buildings considerably damaged, some collapse\")\n",
    "if richter >= 4.5:\n",
    "    print(\"Damge to poorly constrcuted buildings\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Now the alternatives are no longer exclusive. If <font color='blue'>**richter**</font> is 7.1, then the last three tests all match, and three messages are printed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Multiple Alternatives"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#  This program prints a description of an earthquake, given the Richter scale \n",
    "#  magnitude.\n",
    "# Obtain the user input.\n",
    "richter = float(input(\"Enter a magnitude on the Richter scale: \"))\n",
    "# Print the description\n",
    "if richter >= 8.0 :\n",
    "   print(\"Most structures fall\")\n",
    "elif richter >= 7.0 :\n",
    "   print(\"Many buildings destroyed\")\n",
    "elif richter >= 6.0 :\n",
    "   print(\"Many buildings considerably damaged, some collapse\")\n",
    "elif richter >= 4.5 :\n",
    "   print(\"Damage to poorly constructed buildings\")\n",
    "else : \n",
    "   print(\"No destruction of buildings\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Boolean Variables and Operators\n",
    "- Sometimes, you need to evaluate a logical condition in one part of a program and use it elsewhere.\n",
    "- To store a condition that can be true or false, you use a <font color='blue'>**Boolean variable**</font>.\n",
    "    - A boolean variable is also called a flag because it can be either up (<font color='blue'>**True**</font>) or down (<font color='blue'>**False**</font>). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- In Python, the <font color='blue'>**bool**</font> data type has exactly two values, denoted `False` and `True`. \n",
    "- These values are not strings or integers; they are special values, just for <font color='blue'>**Boolean variables**</font>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "flag = True\n",
    "print(\"The variable flag is of type\", type(flag))\n",
    "if flag :\n",
    "    print (\"A flag is raised\")\n",
    "else :\n",
    "    print(\"No flag is raised\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Logical `and` and `or` Operators\n",
    "- Suppose you write a program that processes temperature values, and you want\n",
    "to test whether a given temperature corresponds to liquid water. \n",
    "    - (At sea level, water freezes at 0 degrees Celsius and boils at 100 degrees.) \n",
    "    - Water is liquid if the temperature is greater than zero and less than 100:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "temp = 10\n",
    "if temp > 0 and temp < 100:\n",
    "    print(\"Liquid\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The condition of the test has two parts, joined by the <font color=\"blue\"> **and**</font> operator. \n",
    "- Each part is a Boolean value that can be `True` or `False`. \n",
    "- The combined expression is `True` if both individual expressions are `True`. \n",
    "- If either one of the expressions is `False`, then the result is also `False`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Similarly, we have the \n",
    "<font color=\"blue\"> **or**</font> operator. \n",
    "- A combined expression with the <font color=\"blue\"> **or**</font> operator is `True` if at least one of them is `True`. \n",
    "- If both expressions are `False`, then the result is also `False`."
   ]
  },
  {
   "attachments": {
    "fig13.png": {
     "image/png": "[encoded data, 38116 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Truth Tables for the Logical Operators\n",
    "![fig13.png](attachment:fig13.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Python utilizes **short circuit evaluation** when evaluating expressions involving the logical operators <font color=\"blue\"> **and**</font> and <font color=\"blue\"> **or**</font>\n",
    "    - If the first expression of the <font color=\"blue\"> **and**</font> evaluates to `False`, the second expression is not evaluated.\n",
    "    - Similarly for the expressions of the <font color=\"blue\"> **or**</font> operator. Can you determine how?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Consider the following example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "value1 = 6\n",
    "value2 = 10\n",
    "value3 = 0\n",
    "if value1 > value2 and value2/value3 > 1 :\n",
    "    print(\"This evaluates to True\")\n",
    "else :\n",
    "    print(\"This evaluates to False, but at least, it does not crash\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Conditions \"Values\"\n",
    "- A condition in an `if` statement can be `True` or `False`, for example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(4 < 5)\n",
    "print(4 >= 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- A condition in an `if` statement that has non-zero value is always considered to be true, although its value is not a Boolean type. As a result, a *zero* of *any type* is considered false."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "if 3:\n",
    "    print('test1')\n",
    "if 0:\n",
    "    print('test2')\n",
    "if 0.0101:\n",
    "    print('test3')\n",
    "if 0.000000 :\n",
    "    print('test4')\n",
    "if \"\" :\n",
    "    print('test5')\n",
    "if not \"\" :\n",
    "    print('test6')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Boolean Variables and Operators - Student Activity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Let us test whether water is not liquid at a given temperature. \n",
    "- That is the case when the temperature is at most 0 or at least 100. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Student Activity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#  This program demonstrates comparisons of numbers, using Boolean expressions.\n",
    "x = float(input(\"Enter a number (such as 3.5 or 4.5): \"))\n",
    "y = float(input(\"Enter a second number: \"))\n",
    "\n",
    "if x == y : \n",
    "   print(\"They are the same.\")\n",
    "else : \n",
    "   if x > y :\n",
    "      print(\"The first number is larger\")\n",
    "   else :\n",
    "      print(\"The first number is smaller\")\n",
    "\n",
    "   if -0.01 < x - y and x - y < 0.01 :\n",
    "      print(\"The numbers are close together\")\n",
    "\n",
    "   if x > 0 and y > 0 or x < 0 and y < 0 :\n",
    "      print(\"The numbers have the same sign\")\n",
    "   else :\n",
    "      print(\"The numbers have different signs\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Analyzing Strings\n",
    "- Sometimes it is necessary to determine if a string contains a given **substring**.\n",
    "    - i.e., one string contains an exact match of another string. \n",
    "- for example, given the code segment,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name = \"John Wayne\"\n",
    "print(\"Way\" in name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Python also provides the inverse of the `in` operator, **`not in`**"
   ]
  },
  {
   "attachments": {
    "fig17.PNG": {
     "image/png": "[encoded data, 143296 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Operators for Testing Substrings\n",
    "![fig17.PNG](attachment:fig17.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name = \"John Johnson\"\n",
    "print(\"john\" in \"John Johnson\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"ho\" not in name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(name.count(\"oh\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(name.find(\"oh\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(name.find(\"ho\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(name.startswith(\"john\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": []
  },
  {
   "attachments": {
    "fig18.PNG": {
     "image/png": "[encoded data, 186984 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Methods for Testing String Characteristics\n",
    "![fig18.PNG](attachment:fig18.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name = \"John Johnson\"\n",
    "name.isspace()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name.isalnum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "\"1729\".isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "\"-1729\".isdigit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Input Validation\n",
    "- An important application for the if statement is input validation. \n",
    "- Whenever your program accepts user input, you need to make sure that the user-supplied values are valid before you use them in your computations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Consider our elevator simulation program. \n",
    "- Assume that the elevator panel has buttons labeled 1 through 20 (but not 13). - The following are illegal inputs:\n",
    "    - The number 13\n",
    "    - Zero or a negative number\n",
    "    - A number larger than 20\n",
    "    - An input that is not a sequence of digits, such as five."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Input Validation\n",
    "- In each of these cases, we will want to give an error message and exit the program.\n",
    "- It is simple to guard against an input of 13:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "floor = 13\n",
    "if floor == 13:\n",
    "    print(\"Error: There is no thirteen floor.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Here is how you ensure that the user does not enter a number outside the valid range:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "floor = -1\n",
    "if floor <=0 or floor > 20:\n",
    "    print(\"Error: The floor must be between 1 and 20.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Input Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#  This program simulates an elevator panel that skips the 13th floor, \n",
    "#  checking for input errors.\n",
    "# Obtain the floor number from the user as an integer.\n",
    "floor = int(input(\"Floor: \"))\n",
    "   \n",
    "# Make sure the user input is valid.\n",
    "if floor == 13 :\n",
    "   print(\"Error: There is no thirteenth floor.\")\n",
    "elif floor <= 0 or floor > 20 :\n",
    "   print(\"Error: The floor must be between 1 and 20.\")    \n",
    "else :    \n",
    "   # Now we know that the input is valid\n",
    "   actualFloor = floor\n",
    "   if floor > 13 :\n",
    "      actualFloor = floor - 1\n",
    "      \n",
    "   print(\"The elevator will travel to the actual floor\", actualFloor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Summary: if Statement\n",
    "- The if statement allows a program to carry out different actions depending on the nature of the data to be processed.\n",
    "- Relational operators ( < <= > >= == != ) are used to compare numbers and Strings.\n",
    "- Multiple if statements can be combined to evaluate complex decisions.\n",
    "- When using multiple if statements, test general conditions after more specific conditions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Summary: Boolean\n",
    "- The type `bool` has two values, `True` and `False`.\n",
    "    - Python has two Boolean operators that combine conditions: `and` and `or`.\n",
    "    - To invert a condition, use the `not` operator. \n",
    "    - When checking for equality use the ! operator.\n",
    "- The and and or operators are computed lazily: \n",
    "    - As soon as the truth value is determined, no further conditions are evaluated."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "rise": {
   "theme": "yellow"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "789px",
    "left": "1536px",
    "top": "118px",
    "width": "384px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
